//
//  Date+UseExt.swift
//  jrw
//
//  Created by 赵家彬 on 2017/10/19.
//  Copyright © 2017年 武汉玖信普惠金融信息服务有限公司. All rights reserved.
//

import Foundation


extension Date {
    var normalComponent:DateComponents {
        get {
            let calendar = NSCalendar.current
            let componentsSet = Set<Calendar.Component>([.year, .month, .day])
            var comp = calendar.dateComponents(componentsSet, from: self)
            comp.timeZone = TimeZone(abbreviation: "CCT")
            return comp
        }
    }
    
    var normalString:String {
        get {
            let timeZone = TimeZone.init(identifier: "UTC")
            let formatter = DateFormatter()
            formatter.timeZone = timeZone
            formatter.locale = Locale.init(identifier: "zh_CN")
            formatter.dateFormat = "yyyy-MM-dd"
            return formatter.string(from: self)
        }
    }
    
    func toString(format:String) -> String {
        let timeZone = TimeZone.init(identifier: "UTC")
        let formatter = DateFormatter()
        formatter.timeZone = timeZone
        formatter.locale = Locale.init(identifier: "zh_CN")
        formatter.dateFormat = format
        return formatter.string(from: self)
    }
}

extension DateComponents {
    var normalDate:Date {
        mutating get {
            let calendar = NSCalendar.current
            self.timeZone = TimeZone(abbreviation: "GMT")
            return calendar.date(from: self) ?? Date()
        }
    }
}

